<?php

namespace App\Http\Controllers;

use App\Activity;
use App\Http\Requests\AddActivityRequest;
use App\MakeTable\Table;
use App\Redict\Success;
use App\SoftImage\SoftImage;
use Illuminate\Http\Request;

class ActivityController extends Controller
{
    public function __construct()
    {
        $this->middleware('can:root_Admin_avtivity');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $pagename = '活动管理';
        $modelurl = 'activities';
        $tablecate = ['tablecate'=>'活动'];
        $headname = ['活动名称','活动地址','活动时间','参与人数','价格','联系电话','封面图片','简介','操作'];
        $tablehead = ['name','address','activity_time','part_count','price','contact_tele','cover_image','intro','id'];
        $activitiestable = new Table($tablehead,$page = 8);
        $model = $activitiestable->viewdata(Activity::class);
        $models = $activitiestable->viewpagedata(Activity::class);
        return view('admin.home.tableshow',compact('tablecate','model','tablehead','models','headname','pagename','modelurl'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //新增活动
        $modelurl = 'activities';
        $pagename = '新增活动';
        $formhead = ['name','address','activity_time','price','contact_tele','cover_image','intro'];
        $formname = ['活动名称','活动地址','活动时间','价格','联系电话','封面图片','简介'];
        $formhead = collect($formname)->zip($formhead)->toArray();//压缩成数组
        return view('admin.home.activityform',compact('pagename','tags','modelurl','formhead'));
        //表单，位置 resources/views/admin/home/activityform.blade
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(AddActivityRequest $request)
    {
        //添加验证
        $data = array_merge(
            $request->all(),
            SoftImage::cover($request->file('cover_image'))//处理图片的函数
        );
        //dd($data);调试打印
        $attivity = Activity::create($data);
        return redirect('admin/activities')->with('message', '成功添加'.$attivity->name);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $activity = Activity::findOrFail($id);
        $modelurl = 'activities';
        $pagename = $activity->name;
        $formhead = ['name','address','activity_time','price','contact_tele','cover_image','intro'];
        $formname = ['活动名称','活动地址','活动时间','价格','联系电话','封面图片','简介'];
        $formhead = collect($formname)->zip($formhead)->toArray();
        return view('admin.home.editactivity',compact('pagename','activity','formhead','modelurl'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        //添加验证
        $activity = Activity::findOrFail($id);
        //查找
        $data = array_merge(
            $request->all()
        );
        if($request->file('cover_image')){
            $data = array_merge($data, SoftImage::cover($request->file('cover_image')));
        }
        $activity->update($data);
        //更新
        return Success::back();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //删除
        Activity::destroy($id);
        return Success::back();
    }
}
